home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0023_Positioning Text File.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-05-25  |  2.6 KB  |  102 lines

  1.  
  2. Unit TextUtil;
  3. { Written by Wilbert Van.Leijen and posted in the Pascal Echo }
  4.  
  5. Interface
  6.  
  7. Function TextFilePos(Var f : Text) : LongInt;
  8. Function TextFileSize(Var f : Text) : LongInt;
  9. Procedure TextSeek(Var f : Text; n : LongInt);
  10.  
  11. Implementation
  12. uses Dos;
  13.  
  14. {$R-,S- }
  15.  
  16. Procedure GetFileMode; Assembler;
  17.  
  18. ASM
  19.         CLC
  20.         CMP    ES:[DI].TextRec.Mode, fmInput
  21.         JE     @1
  22.         MOV    [InOutRes], 104         { 'File not opened for reading' }
  23.         XOR    AX, AX                  { Zero out function result }
  24.         XOR    DX, DX
  25.         STC
  26. @1:
  27. end;  { GetFileMode }
  28.  
  29. Function TextFilePos(Var f : Text) : LongInt; Assembler;
  30.  
  31. ASM
  32.         LES    DI, f
  33.         CALL   GetFileMode
  34.         JC     @1
  35.  
  36.         XOR    CX, CX                  { Get position of file pointer }
  37.         XOR    DX, DX
  38.         MOV    BX, ES:[DI].TextRec.handle
  39.         MOV    AX, 4201h
  40.         INT    21h                     { offset := offset-BufEnd+BufPos }
  41.         XOR    BX, BX
  42.         SUB    AX, ES:[DI].TextRec.BufEnd
  43.         SBB    DX, BX
  44.         ADD    AX, ES:[DI].TextRec.BufPos
  45.         ADC    DX, BX
  46. @1:
  47. end;  { TextFilePos }
  48.  
  49.  
  50. Function TextFileSize(Var f : Text) : LongInt; Assembler;
  51.  
  52. ASM
  53.         LES    DI, f
  54.         CALL   GetFileMode
  55.         JC     @1
  56.         XOR    CX, CX                  { Get position of file pointer }
  57.         XOR    DX, DX
  58.         MOV    BX, ES:[DI].TextRec.handle
  59.         MOV    AX, 4201h
  60.         INT    21h
  61.         PUSH   DX                      { Save current offset on the stack }
  62.         PUSH   AX
  63.         XOR    DX, DX                  { Move file pointer to EOF }
  64.         MOV    AX, 4202h
  65.         INT    21h
  66.         POP    SI
  67.         POP    CX
  68.         PUSH   DX                      { Save EOF position }
  69.         PUSH   AX
  70.         MOV    DX, SI                  { Restore old offset }
  71.         MOV    AX, 4200h
  72.         INT    21h
  73.         POP    AX                      { Return result}
  74.         POP    DX
  75. @1:
  76. end;  { TextFileSize }
  77.  
  78. Procedure TextSeek(Var f : Text; n : LongInt); Assembler;
  79.  
  80. ASM
  81.         LES    DI, f
  82.         CALL   GetFileMode
  83.         JC     @2
  84.  
  85.         MOV    CX, Word Ptr n+2        { Move file pointer }
  86.         MOV    DX, Word Ptr n
  87.         MOV    BX, ES:[DI].TextRec.Handle
  88.         MOV    AX, 4200h
  89.         INT    21h
  90.         JNC    @1                      { Carry flag = reading past EOF }
  91.         MOV    [InOutRes], AX
  92.         JMP    @2
  93.  
  94.  
  95.         { Force read next time }
  96. @1:     MOV    AX, ES:[DI].TextRec.BufEnd
  97.         MOV    ES:[DI].TextRec.BufPos, AX
  98. @2:
  99. end;  { TextSeek }
  100. end.  { TextUtil }
  101.  
  102.